/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bbcnet.awesomeplayer;

/**
 *
 * @author esingn
 */

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.event.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Java ;) Yupiii
 * @author esingn
 * :D
 */
public class Gui extends JFrame implements ActionListener, MouseListener,DocumentListener{
    JMenuBar menuLeiste = new JMenuBar();
    JMenu menu[] = new JMenu[4];
    JMenuItem menuItem[] = new JMenuItem[10];
    JPanel left = new JPanel();
    XML xml = XML.getInstance();
    Wiedergabe player = new Wiedergabe();
    Table listenGUI = new Table(player,this);
    JPanel search_playlist = new JPanel();
    JPanel search = new JPanel();
    JPanel panel_playlist = new JPanel();
    JTextField field_search = new JTextField();
    String[] s_playlist;
    JList jl_playlist = new JList();
    String eingeloggtAls = "";
    public static boolean datentraeger;
    
    public Gui(){
        super("Awesome Player v1.0 | Nicolo Singer | Fabian Feller | Stefan Burger");
        //instanzieren ovn player
        player.setTable(listenGUI);
        //Hauptmenupunkte definieren
        menu[0] = new JMenu("Datei");
        menu[1] = new JMenu("Playlist");
        menu[2] = new JMenu("Hilfe");
        menu[3] = new JMenu("A-Player");
        
        //Untermenu von M1 definieren
        menuItem[0] = new JMenuItem("Importieren");
        menuItem[1] = new JMenuItem("Synchronisieren");
        
        //Untermenu von M2 definieren
        menuItem[2] = new JMenuItem("Erstellen");
        menuItem[3] = new JMenuItem("Löschen");
        menuItem[4] = new JMenuItem("Sichern");
        menuItem[8] = new JMenuItem("Einloggen");
        menuItem[9] = new JMenuItem("Ausloggen");
        
        //Untermenu von M3 definieren
        menuItem[5] = new JMenuItem("Hilfe anzeigen");
        
        //Untermenu von M4 definieren
        menuItem[6] = new JMenuItem("Credits");
        menuItem[7] = new JMenuItem("Design");
        
        //M1 Untermenupunkte adden
        menu[0].add(menuItem[0]);
        menu[0].add(menuItem[1]);
        
        //M2 Untermenupunkte adden
        menu[1].add(menuItem[2]);
        menu[1].add(menuItem[3]);
        menu[1].add(menuItem[4]);
        menu[1].add(menuItem[8]);
        menu[1].add(menuItem[9]);
        
        //M3 Untermenupunkte adden
        menu[2].add(menuItem[5]);
        
        //M4 Untermenupunkte adden
        menu[3].add(menuItem[7]);
        menu[3].add(menuItem[6]);
        
        //Actionlistener hinzufügen
        menuItem[0].addActionListener(this);
        menuItem[1].addActionListener(this);
        menuItem[2].addActionListener(this);
        menuItem[3].addActionListener(this);
        menuItem[4].addActionListener(this);
        menuItem[5].addActionListener(this);
        menuItem[6].addActionListener(this);
        menuItem[7].addActionListener(this);
        menuItem[8].addActionListener(this);
        menuItem[9].addActionListener(this);
        
        //Der Menubar die einzelnen Menus adden
        menuLeiste.add(menu[0]);
        menuLeiste.add(menu[1]);
        menuLeiste.add(menu[2]);
        menuLeiste.add(menu[3]);
                
        //ListenGUI wird hier verwaltet

        
        //search
        search.setLayout(new BorderLayout(10,0));
        search.add(field_search,BorderLayout.CENTER);
        field_search.getDocument().addDocumentListener(this);
        
        //search_playlist panel
        search_playlist.setLayout(new BorderLayout());
        search_playlist.add(search,BorderLayout.NORTH);
        search_playlist.add(panel_playlist,BorderLayout.CENTER);
        
        //suchen 
        field_search.setPreferredSize(new Dimension(200,30));
        
        //panel_playlist
        //update nimt die versteckten playlist raus
        s_playlist = xml.getPlaylists();
        jl_playlist.setListData(s_playlist);
        jl_playlist.setFixedCellHeight(16);
        jl_playlist.setFixedCellWidth(150);
        jl_playlist.addMouseListener(this);
        jl_playlist.setBackground(new Color(240,240,240));
        panel_playlist.add(jl_playlist);
        
        //Left Panel beinhaltet: player, suche und playlist
        left.setLayout(new BorderLayout());
        
        //add von leftPanel
        left.add(player,BorderLayout.NORTH);
        left.add(search_playlist,BorderLayout.CENTER);

        //ContentPane werden sachen hinzugefügt
        Container cP = this.getContentPane();
        cP.setLayout(new BorderLayout());
        cP.add(menuLeiste, BorderLayout.NORTH);
        cP.add(left, BorderLayout.WEST);
        cP.add(listenGUI, BorderLayout.CENTER);
        
        //Vollbild
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //MusicIcon wird gesetz
        Image img = new ImageIcon(getClass().getResource("/ch/bbcnet/awesomeplayer/files/logo2.png")).getImage();
        setIconImage(img);
        setVisible(true);
        
        
        
        Thread th = new Thread(){
            @Override
            public void run(){               
                File file[] = File.listRoots();
                    while(true){
                    File lb[] = File.listRoots();
                    if(file.length==lb.length){
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException ex) {
                            
                        }                       
                    }else if(file.length<lb.length){
                                    int chosen = JOptionPane.showOptionDialog(null, "Medium erkannt, jetzt Synchronisieren?", "Wechseldatenträger", 0, WIDTH, null, null,null);
                                    if(chosen==0){
                                        new Sync();
                                        datentraeger = true;
                                        file = lb;
                                    }
                                    else{
                                        file = lb;
                                    }                     
                    }
                    else{
                        file = File.listRoots();
                    }
                    
                }
            }
                
            
            
        };
        th.start();
    }
    
    
    public void manuelSearch(){
        Object[][] searchPlaylist = xml.fetchByString(field_search.getText(),listenGUI.getNowShowing());
        listenGUI.setList(searchPlaylist, listenGUI.getNowShowing());
    }
    
    public void updatePlaylistList(){
        jl_playlist.setListData(xml.getPlaylists());
    }
    public void login(String a){
        eingeloggtAls = a;
        String[] tmp = xml.getPlaylists();
        String[] tmp_ = xml.getLockedPlaylists(a);
        int sum = tmp.length+tmp_.length;
        String [] finalEdition = new String[sum];
        for(int i=0;i<tmp.length;i++){
            finalEdition[i] = tmp[i];
        }
        for(int i=tmp.length;i<finalEdition.length;i++){
            finalEdition[i] = tmp_[i-tmp.length];
        }
        jl_playlist.setListData(finalEdition);
        this.repaint();
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == menuItem[0]){
            new ImportMusic(listenGUI);
        }
        if(e.getSource() == menuItem[1]){
            new Sync();
        }
        if(e.getSource() == menuItem[2]){
            String name = JOptionPane.showInputDialog(null, "Name der neuen Playliste:");
            if(name != null){
                xml.createPlaylist(name);
                login(eingeloggtAls);
            }
            
        }
        if(e.getSource() == menuItem[3]){
            String name = JOptionPane.showInputDialog(null, "Name der zu löschenden Playliste:");
            if(name != null){
            	if(xml.getOwner(name).equals(eingeloggtAls)){
            		xml.deletePlaylist(name);
            	}
                
                
            }
            login(eingeloggtAls);
        }
        if(e.getSource() == menuItem[4]){
            new PlaylistSafer(this,true);
            
        }
        if(e.getSource() == menuItem[5]){
            JOptionPane.showMessageDialog(this, "In der Technischen Dokumentation finden Sie ab Seite 56 \nein Benutzerhandbuch. Kommen Sie auch\nmit dieser nicht weiter, wenden Sie sich nicht\nan den Technischen support, sondern\nan Ihren Psychiater.");
        }
        if(e.getSource() == menuItem[6]){
            
            JOptionPane.showMessageDialog(null, "----Copyright----\n-Nicolo Singer\t\n\t->Twitter: @Tuxes3\n-Fabian Feller\n-Stefan Burger\n----©2012----");
        }
        if(e.getSource() == menuItem[7]){
            new ColorChooser(listenGUI);
        }
        if(e.getSource() == menuItem[8]){
            new PlaylistSafer(this,false);
        }
        if(e.getSource() == menuItem[9]){
            eingeloggtAls = "";
            login(eingeloggtAls);
        }
       
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==jl_playlist){
            if(e.getButton()==1){
                String tmps = jl_playlist.getSelectedValue().toString();
                Object[][] tmpo = xml.getData(tmps);
                listenGUI.setList(tmpo, tmps);
                listenGUI.updateUI();
            }

                    
        }
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        Object[][] searchPlaylist = xml.fetchByString(field_search.getText(),listenGUI.getNowShowing());
        listenGUI.setList(searchPlaylist, listenGUI.getNowShowing());
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        Object[][] searchPlaylist = xml.fetchByString(field_search.getText(),listenGUI.getNowShowing());
        listenGUI.setList(searchPlaylist, listenGUI.getNowShowing());
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        Object[][] searchPlaylist = xml.fetchByString(field_search.getText(),listenGUI.getNowShowing());
        listenGUI.setList(searchPlaylist, listenGUI.getNowShowing());
    }
}
